package com.ssps.back.manage.permission.controller;

import com.ssps.back.manage.permission.service.Manager_PermissionService;
import com.ssps.entity.Permission;
import com.ssps.entity.Role;
import com.ssps.entity.RolePermissionKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    Manager_PermissionService manager_permissionService;

    @RequestMapping("showPermission")
    @ResponseBody
    public Map<String, Object> showPermission(Integer rid) {
        Map<String, Object> map = new HashMap<>();

        try {
            StringBuilder str = new StringBuilder();
            List<RolePermissionKey> list = manager_permissionService.getByRid(rid);
            if (list.size() != 0) {
                for (RolePermissionKey rolePermissionKey : list) {

                    str.append(rolePermissionKey.getPmid()).append(",");
                }
                String pids = str.substring(0, str.length() - 1);

                map.put("pids", pids);
            }
            map.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        }
        return map;
    }

    @RequestMapping("assignDo")
    @ResponseBody
    public Map<String, Object> assignDo(Integer rid, String pids) {
        Map<String, Object> map = new HashMap<>();
        try {
            manager_permissionService.assign(rid, pids);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 删除权限
     *
     * @param pid
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Map<String, Object> delete(Integer pid) {
        Map<String, Object> map = new HashMap<>();
        try {
            manager_permissionService.delete(pid);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 更新名称
     *
     * @param pid
     * @param name
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public Map<String, Object> update(Integer pid, String name) {
        Map<String, Object> map = new HashMap<>();
        try {
            manager_permissionService.update(pid, name);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 增加权限
     *
     * @param pid
     * @param name
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> add(Integer pid, String pname) {
        Map<String, Object> map = new HashMap<>();
        try {
            System.out.println(pname);
            manager_permissionService.add(pid, pname);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    @RequestMapping("RoleIndex")
    @ResponseBody
    public Map<String, Object> RoleIndex() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Role> list = manager_permissionService.selectAllRoles();
            map.put("roleList", list);
            map.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        }
        return map;
    }

    @RequestMapping("assignPermissionShow")
    @ResponseBody
    public List<Permission> assignPermissionShow() {
        List<Permission> list = manager_permissionService.list();
        return manager_permissionService.list();
    }

    @RequestMapping("assignPermission")
    public String assignPermission(Integer rid, Model model) {
        model.addAttribute("rid", rid);
        return "/manageWeb/AssignPermission";
    }

    @RequestMapping("index")
    public String index() {
        return "/manageWeb/index";
    }

    @RequestMapping("Roleshow")
    public String Roleshow() {
        return "/manageWeb/RoleIndex";
    }
}
